package com.topnav.mc.document.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.js.lib.annotation.ApiLogger;
import com.js.lib.result.Result;
import com.topnav.mc.admin.security.PermissionGrade;
import com.topnav.mc.admin.service.IDeviceService;
import com.topnav.mc.admin.service.ISiteService;
import com.topnav.mc.admin.service.IUserService;
import com.topnav.mc.admin.utils.SystemLogUtil;
import com.topnav.mc.base.CurdController;
import com.topnav.mc.base.mc.McController;
import com.topnav.mc.document.entity.Document;
import com.topnav.mc.document.entity.bean.DocumentBean;
import com.topnav.mc.document.entity.param.DocumentParam;
import com.topnav.mc.document.entity.wrapper.DocumentWrapper;
import com.topnav.mc.document.service.IDocumentService;
import com.topnav.mc.exception.ExceptionMsg;
import com.topnav.mc.uitls.*;
import com.topnav.mc.validator.group.IDelete;
import com.topnav.mc.validator.group.IUpdate;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Date;

@Api(value = "doc/document", tags = "文件管理")
@RestController
@CrossOrigin
@RequestMapping("doc/document")
public class DocumentController extends McController<Document, DocumentBean, DocumentParam,
        IDocumentService, DocumentWrapper> {
    @Autowired
    private IDocumentService documentService;
    @Autowired
    private ISiteService siteService;
    @Autowired
    private IDeviceService deviceService;
    @Autowired
    private IUserService userService;
    @Autowired
    private SystemLogUtil systemLogUtil;
    @Resource
    private PermissionGradeUtil permissionGradeUtil;


    @ApiOperation(value = "删除")
    @PostMapping("/delete")
    @ResponseBody
    @Override
    public Result delete(@Validated(IDelete.class) @RequestBody Document data) {
        PermissionGrade permissionGrade = permissionGradeUtil.getPmGrade();
        if (permissionGrade == null){
            return ResultErrorUtil.byExceptionMsg(ExceptionMsg.ERROR_AUTHENTICATION_FAILE);
        }

        if (permissionGrade.getPermissionLevel() < 4){
            //throw ErrorResultUtil.result(ExceptionMsg.ERROR_DELETE_FILE_ONLAY_ADMIN_FAILED);
            throw ExceptionUtil.commonException(ExceptionMsg.ERROR_DELETE_FILE_ONLAY_ADMIN_FAILED);
        }

        UpdateWrapper<Document> updateWrapper = new UpdateWrapper<>();
        updateWrapper.set("delete_flag",true)
                .set("update_time",new Date())
                .set("update_by",permissionGrade.getUserName())
                .eq("id",data.getId());
        if (documentService.update(updateWrapper)){
            return Result.ok("文件删除成功！");
        }else {
            return Result.ok("文件删除失败！");
        }
    }

    /**
     * 修改，只修改几个字段
     * id != null
     * @param data
     * @return
     */
    @ApiLogger(apiName="/update")
    @PostMapping("/update")
    @ResponseBody
    @Override
    public Result update(@Validated(IUpdate.class) @RequestBody Document data) {
        UpdateWrapper<Document> updateWrapper = new UpdateWrapper<>();
        updateWrapper.set("importance",data.getImportance()).set("remark",data.getRemark()).set("expire_time",data.getExpireTime())
                .set("update_time",new Date())
                .eq("id",data.getId());
        if (documentService.update(updateWrapper)){
            return Result.ok("文件标注成功！");
        }else {
            return Result.ok("文件标注失败！");
        }
    }


    /**
     * 自定义分页查询
     * @param p
     * @return
     */
    @ApiLogger(apiName="/query/searchPage")
    @ApiOperation(value = "文件分页查询")
    @PostMapping("/query/searchPage")
    @ResponseBody
    public Result searchPage(@Validated(IPage.class) @RequestBody QueryPage<Document> p) {
        Page datas = documentService.searchDocument(p.getPage(),p.getWhere());
        return Result.ok(datas);
    }

    /**
     * 文件下载
     * @param id
     * @return
     */
    @ApiLogger(apiName="doc/document/download/file/{id}")
    @ApiOperation(value = "文件下载")
    @GetMapping("/download/file/{id}")
    @ResponseBody
    public Result download(@PathVariable Long id, HttpServletRequest request, HttpServletResponse response) {


        /*Page datas = documentService.searchDocument(p.getPage(),p.getWhere());*/
        return Result.ok();
    }

    @ApiLogger(apiName="doc/document/upload/file")
    @ApiOperation(value = "文件上传")
    @GetMapping("/upload/file/{id}")
    @ResponseBody
    public Result upload(@PathVariable Long id, HttpServletRequest request, HttpServletResponse response) {


        /*Page datas = documentService.searchDocument(p.getPage(),p.getWhere());*/
        return Result.ok();
    }


}
